package com.community.web.controller.student;

import com.community.common.annotation.Log;
import com.community.common.core.controller.BaseController;
import com.community.common.core.domain.AjaxResult;
import com.community.common.core.page.TableDataInfo;
import com.community.common.enums.BusinessType;
import com.community.common.utils.StringUtils;
import com.community.domain.StudentEntity;
import com.community.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController extends BaseController{
    @Autowired
    private StudentService studentService;
    @PreAuthorize("@ss.hasPermi('student:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudentEntity student) {
        startPage();
        List<StudentEntity> list=studentService.selectStudentList(student);
        return getDataTable(list);

    }
    @PreAuthorize("@ss.hasPermi('student:student:add')")
    @PostMapping("/addStudent")
    public AjaxResult addStudent(@RequestBody StudentEntity student) {
        return toAjax(studentService.addStudent(student));
    }

    @PreAuthorize("@ss.hasPermi('student:student:edit')")
    @Log(title ="修改", businessType = BusinessType.UPDATE)
    @PostMapping("/updStudent")
    public AjaxResult updStudent(@Validated @RequestBody StudentEntity student)
    {
        System.out.println(student.getName());
        student.setUpdateBy(getUsername());
        return toAjax(studentService.updStudent(student));
    }

    @PreAuthorize("@ss.hasPermi('student:student:query')")
    @GetMapping("/query/{id}")
    public AjaxResult authRole(@PathVariable("id") int id)
    {
        AjaxResult ajax = AjaxResult.success();
        StudentEntity student = studentService.selectStudentInfoById(id);
//    List<SysRole> roles = roleService.selectRolesByUserId(userId);
        ajax.put("student", student);
//    ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        return ajax;
    }
    @PreAuthorize("@ss.hasPermi('student:student:remove')")
    @DeleteMapping ("/{id}")
    public AjaxResult delStudent(@PathVariable String id) {
        if(StringUtils.isEmpty(id)){
            return AjaxResult.error("参数为空");
        }
        return toAjax(studentService.delStudent(Integer.valueOf(id)));
    }


}
